package com.oxy.action.util;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.oxy.common.constant.ActionConstant;
import com.oxy.model.Pager;
import com.oxy.util.Oxy;


public class ActionUtil {
	private final static Logger logger = Oxy.getLogManager().getLoggerCommon();

	/*
	 * Request
	 */
	public static HttpServletRequest getR(){
		return ServletActionContext.getRequest();
	}
	/*
	 * Response
	 */
	public static HttpServletResponse getP(){
		return ServletActionContext.getResponse();
	}
	/*
	 * Session
	 */
	public static HttpSession getS(){
		return ServletActionContext.getRequest().getSession();
	}
	/*
	 * Cookie
	 */
	public static Cookie[] getC(){
		return ServletActionContext.getRequest().getCookies();
	}
	
	public static String getRParam(String paraNm){
		return ServletActionContext.getRequest().getParameter(paraNm);
	}
	
	public static Object getRAttr(String attrNm){
		return ServletActionContext.getRequest().getAttribute(attrNm);
	}
	public static void setRAttr(String attrNm, Object attrVal){
		ServletActionContext.getRequest().setAttribute(attrNm, attrVal);
	}
	public static void rmRAttr(String attrNm){
		ServletActionContext.getRequest().removeAttribute(attrNm);
	}
	
	public static Object getSAttr(String attrNm){
		return ServletActionContext.getRequest().getSession().getAttribute(attrNm);
	}
	public static void setSAttr(String attrNm, Object attrVal){
		ServletActionContext.getRequest().getSession().setAttribute(attrNm, attrVal);
	}
	public static void rmSAttr(String attrNm){
		ServletActionContext.getRequest().getSession().removeAttribute(attrNm);
	}
	
	public static String getAllRequestParametersStr(){
		StringBuffer ret = new StringBuffer();
		Enumeration en = ActionUtil.getR().getParameterNames();
		while(en.hasMoreElements()){
			String key = (String)en.nextElement();
			ret.append(key+"="+ActionUtil.getRParam(key));
		}
		return ret.toString();
	}
	
	public static void responseWrite(String msg){
		try {
			ServletActionContext.getResponse().getWriter().write(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	public static Pager preparePager(long itemCount){
		Pager pager = new Pager();
		// set page index
		String pageindex = ActionUtil.getRParam(ActionConstant.PAGER.PARA_PAGE_INDEX);
		if(pageindex!=null && !pageindex.equals("")){
			pager.setCurrentPage(Integer.parseInt(pageindex));
		}
		else{
			pager.setCurrentPage(1);
		}
		
		// set page size, if missing, use default
		String pagesize = ActionUtil.getRParam(ActionConstant.PAGER.PARA_PAGE_SIZE);
		if(pagesize!=null && !pagesize.equals("")){
			pager.setPageSize(Integer.parseInt(pagesize));
		}
		else{
			pager.setPageSize(Integer.parseInt(Oxy.getCfgManager().App().PAGER_PAGESIZE));
		}
		
		// set total page
		if(itemCount%pager.getPageSize() == 0){
			pager.setTotalPage((int)(itemCount/pager.getPageSize()));
		}
		else{
			pager.setTotalPage((int)(itemCount/pager.getPageSize()+1));
		}
		
		// if the wrong page index
		if(pager.getCurrentPage()>pager.getTotalPage()){
			pager.setCurrentPage(pager.getTotalPage());
		}
		
		pager.setCurrentPageStartInd((long)pager.getPageSize()*(long)(pager.getCurrentPage()-1));
		pager.setCurrentPageEndInd(pager.getCurrentPageStartInd()+(long)pager.getPageSize());
		
		return pager;
	}
}
